# from scipy.interpolate import CubicHermiteSpline

def cubic_hermite_interpolate(x_points, y_points, custom_derivatives, x_experiment):
    # interpolator = PchipInterpolator(x_points, y_points)
    # return interpolator(x_experiment)
    def hermite_basis_functions(t):
        h00 = (1 + 2 * t) * (1 - t) ** 2
        h10 = t * (1 - t) ** 2
        h01 = t ** 2 * (3 - 2 * t)
        h11 = t ** 2 * (t - 1)
        return h00, h10, h01, h11
    
    def linear_extrapolation(x, x0, y0, x1, y1):
        # 边界的线性外推
        return y0 + ((y1 - y0) / (x1 - x0)) * (x - x0)

    y_experiment = []
    
    for x in x_experiment:
        if x < x_points[0]:
            y_experiment.append(linear_extrapolation(x, x_points[0], y_points[0], x_points[1], y_points[1]))
        elif x > x_points[-1]:
            y_experiment.append(linear_extrapolation(x, x_points[-2], y_points[-2], x_points[-1], y_points[-1]))
        else:
            # 找到适合的区间 
            for i in range(len(x_points) - 1):
                if x_points[i] <= x <= x_points[i + 1]:
                    h = x_points[i + 1] - x_points[i]
                    t = (x - x_points[i]) / h

                    h00, h10, h01, h11 = hermite_basis_functions(t)
                    
                    # Hermite 插值公式
                    interpolated_value = (
                        h00 * y_points[i] +
                        h10 * h * custom_derivatives[i] +
                        h01 * y_points[i + 1] +
                        h11 * h * custom_derivatives[i + 1]
                    )
                    y_experiment.append(interpolated_value)
                    break

    return y_experiment